package com.china08.yxyapi.service.lesson.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.ReqModel;
import com.china08.yxyapi.model.ReqTopicModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.service.lesson.DataSyncService;

@Service
public class DataSyncServiceImpl implements DataSyncService {

    @Autowired
    private YxyLessonsSynchroLessonRepository lessonRepository;

    @Autowired
    private YxyLessonsSynchroCatalogRepository catalogRepository;

    @Autowired
    private YxyLessonsSynchroLessonChapterRepository chapterRepository;

    @Autowired
    private YxyLessonsSynchroLessonItemRepository itemRepository;

    @Autowired
    private YxyLessonsSynchroGradeRepository gradeRepository;

    @Autowired
    private YxyLessonsTopicLessonRepository topicRepository1;

    @Autowired
    private YxyLessonsTopicCatalogRepository catalogRepository1;

    @Autowired
    private YxyLessonsTopicLessonChapterRepository chapterRepository1;

    @Autowired
    private YxyLessonsTopicLessonItemRepository itemRepository1;

    @Autowired
    private YxyLessonsVideoRepository videoRepository1;

    @Transactional
    @Override
    public String saveLesson(ReqModel model) {
        YxyLessonsSynchroLesson lesson = model.getLesson();
        lessonRepository.save(lesson);
        YxyLessonsSynchroCatalog catalog = model.getCatalog();
        catalogRepository.save(catalog);

        List<YxyLessonsSynchroLessonChapter> chapters = model.getChapters();
        if (CollectionUtils.isNotEmpty(chapters)) {
            chapterRepository.save(chapters);
        }
        List<YxyLessonsSynchroLessonItem> items = model.getItems();
        if (CollectionUtils.isNotEmpty(items)) {
            itemRepository.save(items);
        }
        if (model.getGrade() != null) {
            gradeRepository.save(model.getGrade());
        }

        return lesson.getId();

    }

    @Transactional
    @Override
    public String saveTopicLesson(ReqTopicModel model) {
        YxyLessonsTopicLesson lesson = topicRepository1.save(model.getTopic());
        System.out.println(lesson.getId() + "=============");
        if (CollectionUtils.isNotEmpty(model.getCatalogList())) {
            catalogRepository1.save(model.getCatalogList());
        }
        if (CollectionUtils.isNotEmpty(model.getChapters())) {
            chapterRepository1.save(model.getChapters());
        }

        if (CollectionUtils.isNotEmpty(model.getItems())) {
            itemRepository1.save(model.getItems());
        }

        if (CollectionUtils.isNotEmpty(model.getVideos())) {
            videoRepository1.save(model.getVideos());
        }
        return lesson.getId();
    }

}
